package com.bgyrobot.coordinate.model;

import com.alibaba.fastjson.annotation.JSONField;

import lombok.Data;

@Data
public class ReferencePoint {
    
    public static final String hashString(String refCS, double[] refCoord) {
        return String.format("%s[%f,%f,%f]", refCS, refCoord[0], refCoord[1], refCoord[2]);
    }
    
    public static final boolean isEqual(ReferencePoint pt1, ReferencePoint pt2) {
        if (pt1 == null && pt2 == null) {
            return true;
        }
        if (pt1 == null || pt2 == null) {
            return false;
        }
        return hashString(pt1.refCS, pt1.refCoord).equalsIgnoreCase(hashString(pt2.refCS, pt2.refCoord));
    }
    
    /**
     * 参考点坐标系名称
     */
    @JSONField(name="RefCS")
    private String refCS;
    
    /**
     * 参考点坐标
     */
    @JSONField(name="RefCoord")
    private double[] refCoord;
    
    public ReferencePoint(String refCS, double[] refCoord) {
        this.refCS = refCS;
        this.refCoord = refCoord;
    }
    
    public boolean valid() {
        return refCS != null && !refCS.equalsIgnoreCase("") && refCoord != null && refCoord.length == 3;
    }
    
    public ReferencePoint clone() {
        double[] coord = null;
        if (refCoord != null) {
            coord = new double[refCoord.length];
            System.arraycopy(refCoord, 0, coord, 0, refCoord.length);
        }
        return new ReferencePoint(refCS, coord);
    }
    
    @Override
    public String toString() {
        return ReferencePoint.hashString(refCS, refCoord);
    }
    
    @Override
    public boolean equals(Object anObject) {
        if (anObject instanceof ReferencePoint) {
            return ReferencePoint.hashString(refCS, refCoord).equalsIgnoreCase(anObject.toString());
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return ReferencePoint.hashString(refCS, refCoord).hashCode();
    }
}
